package com.example.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.example.pojo.User;
import com.example.pojo.UserAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

//交给spring容器 一会去给springsercurity 框架注入
public class SpringSecuirtyUserService implements UserDetailsService {

    @Reference
    private UserAdminService userService;

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据名字查询信息
        UserAdmin user = userService.findUserByUsername(username);
        if(user==null)
        {//没有获取到用户 直接返回失败
            return null;
        }
        //否则就获取到了信息
        //就看一下角色
        List<GrantedAuthority> list=new ArrayList<GrantedAuthority>();//list是权限列表
        org.springframework.security.core.userdetails.User user1=new org.springframework.security.core.userdetails.User(username,"{noop}"+user.getPassword(),list);
        return user1;


    }
}
